const path = require('path')
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
//最新版本安装后会有异常，目前用的5.0.0
const CompressionPlugin = require("compression-webpack-plugin");

const resolve = dir => {
    return path.join(__dirname, dir)
}


module.exports = {
    publicPath: '/',
    runtimeCompiler:true,
    productionSourceMap:false,
    chainWebpack: config => {
        config.resolve.alias
            .set('@', resolve('src'));
        
        // 删除console.log();
        // 去除文件中console、warnings、debugger无用信息
        config.optimization.minimizer('terser').tap((args) => {
            args[0].terserOptions.compress.drop_console = true
            return args
        });
    },
    assetsDir: 'assets', //静态资源目录(js,css,img,fonts)这些文件都可以写里面
    configureWebpack:config =>{
        // 删除预加载
        // config.plugins.delete('prefetch');

        return {
            plugins:[
                // js文件模块分析
                // new BundleAnalyzerPlugin(),
                // 压缩
                new CompressionPlugin({
                    algorithm: 'gzip',
                    // 待处理的资源匹配规则
                    test: new RegExp('\\.(' + ['js','css'].join('|') + ')$'),
                    // 只处理比这个值大的资源。按字节计算
                    threshold: 10240,
                    // 只有压缩率比这个值小的资源才会被处理
                    minRatio: 0.8
                })
            ],
            externals:{
                'vue': 'Vue',
                'vue-router': 'VueRouter',
                "element-ui": "ELEMENT",
                // 'axios': 'axios'
            }
        }
    },
   
    pages:{
        main:{
            entry:'src/main.ts',
            template:'public/index.html',
            filename:'index.html',
            title:'index'
        },
        about:{
            entry:'src/views/pages/index.ts',
            template:'public/index.html',
            filename:'views/pages/About.html',
            title:'about'
        },

    }
    ,
    devServer: {
        open: true,
        host: '127.0.0.1',
        port: 8080,
        https: false,
        hotOnly: false,
        proxy: {
            '/': {
                target: 'http://127.0.0.1:8081',
                ws: false,
                changeOrigin: true,
                pathRewrite: {
                    '^/': '/'
                }
            }
        }
    }
}